﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BMallDBAccess;
using ServiceStack.Redis;
using System.Diagnostics;

namespace SpiderServer
{
	class Global
	{
		private static string s_redisHost;
		private static int s_redisPort;
		private static PooledRedisClientManager s_RedisPool;

		private static string s_CurlFileName;
		public static string CurlFileName
		{
			get
			{
				if (s_CurlFileName == null)
				{
					s_CurlFileName = System.Configuration.ConfigurationSettings.AppSettings["CurlFileName"];
				}
				return s_CurlFileName;
			}
		}
		
		public static RedisClient GetRedisClient()
		{
			if (s_RedisPool == null)
			{
				s_RedisPool = new PooledRedisClientManager(new string[]{System.Configuration.ConfigurationSettings.AppSettings["redis_connect"]});
			}

			return  (RedisClient)s_RedisPool.GetClient();
			/*
			if(string.IsNullOrEmpty(s_redisHost))
			{
				string[] arr = System.Configuration.ConfigurationSettings.AppSettings["redis_connect"].Split(':');
				s_redisHost = arr[0];
				s_redisPort = Convert.ToInt32(arr[1]);
			}
			return new RedisClient(s_redisHost, s_redisPort);
			 * */
		}

		private static MySqlDBAccess s_db;
		public static MySqlDBAccess DBAccess
		{
			get
			{
				if (s_db == null)
				{
					s_db = MySqlDBAccessManager.GetAccess("spider", "chinavb234123489");
				}
				return s_db;
			}
		}

		public static string GetWeb(string url, string encoding, string cookie)
		{
			ProcessStartInfo Info = new ProcessStartInfo();
			Info.StandardOutputEncoding = System.Text.Encoding.GetEncoding(encoding);
			Info.Arguments = url;
			Info.FileName = Global.CurlFileName;

			Process Proc = new Process();
			Info.UseShellExecute = false;
			Info.RedirectStandardInput = true;
			Info.RedirectStandardOutput = true;
			Info.RedirectStandardError = true;
			Info.CreateNoWindow = false;
			Proc = System.Diagnostics.Process.Start(Info);
			string strGetString = Proc.StandardOutput.ReadToEnd();
			Proc.StandardInput.WriteLine("exit");
			Proc.WaitForExit();
			return strGetString;
		}
	}
}
